import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Iterator;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;


public class TestAddStatementToModel {
	
	final static String ex = "http://liris.cnrs.fr/smartspace.owl#";


	public  Statement convertLocationToStatement(String person,
            String location, Model m) {

    // create a temp model to void trigger "add statement to model" event
    OntModel tempModel = ModelFactory.createOntologyModel();
    tempModel.add(m);
    // create the new person individual
    OntClass student = tempModel.getOntClass(ex + "Student");
    Individual subject = tempModel.createIndividual(ex + person, student);

    // get the Property
    ObjectProperty hasLocatedIn = tempModel.getObjectProperty(ex
                    + "hasLocatedIn");

    // create the new location individual
    OntClass Restaurant = tempModel.getOntClass(ex + "Classroom");
    Individual object = tempModel.createIndividual(ex + location,
                    Restaurant);

    Statement locationStatement = tempModel.createStatement(subject,
                    hasLocatedIn, object);

    tempModel.close();
    return locationStatement;
}


	public static Model addStatementToModel(Statement s, Model m) {
		if (!m.contains(s.getSubject(), s.getPredicate(), s.getObject())) {
			// Remove all statements with this subject-predicate pair
			// System.out.println("Removing existing context data");
			Model tempModel = ModelFactory.createDefaultModel();
			tempModel.add(m);
			StmtIterator sti = tempModel.listStatements(s.getSubject(),
					s.getPredicate(), (RDFNode) null);
			for (Iterator it = sti; it.hasNext();) {
				Statement st = (Statement) it.next();
				// Remove this statement
				m.remove(st);
			}
			tempModel.close();
		}

		m.add(s);
		System.out.println("[ContextManager] - Statement added");
		return m;
	}
}
